package tue.cse.advanceddatabases.project.xml.album;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class Album {

	private Element element;

	public Album(Element element) {
		this.element = element;
	}

	public Album() {
		this.element = DocumentHelper.createElement("album");
	}

	public String getId() {
		return element.attributeValue("id");
	}

	public void setId(String value) {
		if (element.attribute("id") == null)
			element.addAttribute("id", value);
		else
			element.attribute("id").setValue(value);
	}

	public String getArtistId() {
		return element.elementText("artist_id");
	}

	public void setArtistId(String value) {
		if (element.element("artist_id") == null)
			element.addElement("artist_id").setText(value);
		else
			element.element("artist_id").setText(value);
	}

	public String getName() {
		return element.elementText("name");
	}

	public void setName(String value) {
		if (element.element("name") == null)
			element.addElement("name").setText(value);
		else
			element.element("name").setText(value);
	}

	public String getNotes() {
		return null;
	}

	public void setNotes(String value) {

	}

	public String getImage() {
		return element.elementText("image");
	}

	public void setImage(String value) {
		if (element.element("image") == null)
			element.addElement("image").setText(value);
		else
			element.element("image").setText(value);
	}

	public String getDateCreated() {
		return element.elementText("date_created");
	}

	public void setDateCreated(String value) {
		if (element.element("date_created") == null)
			element.addElement("date_created").setText(value);
		else
			element.element("date_created").setText(value);
	}

	public String getYear() {
		return element.elementText("year");
	}

	public void setYear(String value) {
		if (element.element("year") == null)
			element.addElement("year").setText(value);
		else
			element.element("year").setText(value);
	}

	public String getGenre() {
		return element.elementText("genre");
	}

	public void setGenre(String value) {
		if (element.element("genre") == null)
			element.addElement("genre").setText(value);
		else
			element.element("genre").setText(value);
	}

	public String getLaunchdate() {
		return element.elementText("launchdate");
	}

	public void setLaunchdate(String value) {
		if (element.element("launchdate") == null)
			element.addElement("launchdate").setText(value);
		else
			element.element("launchdate").setText(value);
	}

	public String getNumberOfTracks() {
		return element.elementText("numberOfTracks");
	}

	public void setNumberOfTracks(String value) {
		if (element.element("numberOfTracks") == null)
			element.addElement("numberOfTracks").setText(value);
		else
			element.element("numberOfTracks").setText(value);
	}

	public String getRating() {
		if ( (element.elementText("rating") == null) || 
				 (element.elementText("rating-count")==null) ||
				 (element.elementText("rating").length() <= 0) || 
				 (element.elementText("rating-count").length() <=0 ) ||
				  "0".equalsIgnoreCase(element.elementText("rating")) ||
				  "0".equalsIgnoreCase(element.elementText("rating-count"))) {
				return "no";
			}

		long rating = Long.valueOf(element.elementText("rating")).longValue();
		long ratingCount = Long.valueOf(element.elementText("rating-count"))
				.longValue();

		long rateValue = (long) Math.ceil(rating / ratingCount);

		return String.valueOf(rateValue);

	}
	
	public String getCommentCount(){
		return element.elementText("comment-count");
	}
	
	public void setCommentCount(String value) {
		// do nothing. it is automatic done when commenting.
	}
}
